//
//  UIImage+Extension.swift
//  SinaWeiBo
//
//  Created by 毛强 on 16/1/27.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

extension UIImage {
    //屏幕截图
   class func getScreenShot() -> UIImage{
    
        let window = UIApplication.sharedApplication().keyWindow
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(SCREENW, SCREENH), false, 0)
        window?.drawViewHierarchyInRect(CGRect(origin: CGPointZero, size: window!.frame.size), afterScreenUpdates: false)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
    
    func scaleImagWidth(width:CGFloat) -> UIImage{
        
        if width >= self.size.width{
            return self
        }
        
        let height = width/self.size.width * self.size.height
        
        let rect = CGRectMake(0, 0, width, height)
        UIGraphicsBeginImageContext(rect.size)
        
        self.drawInRect(rect)
        
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        return image
    }
}
